home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / find / dist / lib / nextelem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-12  |  2.4 KB  |  100 lines

  1. /* Return the next element of a path.
  2.    Copyright (C) 1992 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by David MacKenzie (djm@gnu.ai.mit.edu),
  19.    inspired by John P. Rouillard (rouilj@cs.umb.edu).  */
  20.  
  21. #include <stdio.h>
  22. #if defined(USG) || defined(STDC_HEADERS)
  23. #include <string.h>
  24. #define index strchr
  25. #else
  26. #include <strings.h>
  27. #endif
  28.  
  29. char *strdup ();
  30.  
  31. /* Return the next element of a colon-separated path.
  32.    A null entry in the path is equivalent to "." (the current directory).
  33.  
  34.    If NEW_PATH is non-NULL, set the path and return NULL.
  35.    If NEW_PATH is NULL, return the next item in the string, or
  36.    return NULL if there are no more elements.  */
  37.  
  38. char *
  39. next_element (new_path)
  40.      char *new_path;
  41. {
  42.   static char *path = NULL;    /* Freshly allocated copy of NEW_PATH.  */
  43.   static char *end;        /* Start of next element to return.  */
  44.   static int final_colon;    /* If zero, path didn't end with a colon.  */
  45.   char *start;            /* Start of path element to return.  */
  46.  
  47.   if (new_path)
  48.     {
  49.       if (path)
  50.     free (path);
  51.       end = path = strdup (new_path);
  52.       final_colon = 0;
  53.       return NULL;
  54.     }
  55.  
  56.   if (*end == '\0')
  57.     {
  58.       if (final_colon)
  59.     {
  60.       final_colon = 0;
  61.       return ".";
  62.     }
  63.       return NULL;
  64.     }
  65.  
  66.   start = end;
  67.   final_colon = 1;        /* Maybe there will be one.  */
  68.  
  69.   end = index (start, ':');
  70.   if (end == start)
  71.     {
  72.       /* An empty path element.  */
  73.       *end++ = '\0';
  74.       return ".";
  75.     }
  76.   else if (end == NULL)
  77.     {
  78.       /* The last path element.  */
  79.       end = index (start, '\0');
  80.       final_colon = 0;
  81.     }
  82.   else
  83.     *end++ = '\0';
  84.  
  85.   return start;
  86. }
  87.  
  88. #ifdef TEST
  89. int
  90. main ()
  91. {
  92.   char *p;
  93.  
  94.   next_element (getenv ("PATH"));
  95.   while (p = next_element (NULL))
  96.     puts (p);
  97.   exit (0);
  98. }
  99. #endif /* TEST */
  100.